package org.osomit.sacct.session.token.impl;

import org.osomit.sacct.session.token.iface.TokenExpirationPolicy;
import org.osomit.sacct.session.token.iface.TokenState;
import org.osomit.sacct.server.config.iface.ServerConfig;
import com.google.inject.Inject;

/**
 * @author Jian Fang (John.Jian.Fang@gmail.com)
 *
 * Date: Jul 1, 2009
 */
public final class IdleTimeoutTokenExpirationPolicy implements TokenExpirationPolicy {
    private static final long serialVersionUID = -6628028938729089636L;
    
    private final long idletimeInMilliSeconds;

    @Inject
    public IdleTimeoutTokenExpirationPolicy(ServerConfig config){
        this.idletimeInMilliSeconds = config.getIdletimeInMilliSeconds();
    }
    public long getIdletimeInMilliSeconds() {
        return this.idletimeInMilliSeconds;
    }

    public boolean isExpired(TokenState tokenState) {
        return (tokenState == null)
            || (System.currentTimeMillis() - tokenState.getLastTimeUsed() >= idletimeInMilliSeconds);
    }
}
